package com.example.customer01.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.customer01.pojo.Student;
import com.example.customer01.service.StudentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 学生管理控制器
 */
@RestController
@RequestMapping("/api/students")
public class StudentController {

    @Resource
    private StudentService studentService;

    /**
     * 创建学生
     */
    @PostMapping
    public Object createStudent(@RequestBody Student student) {
        try {
            Student created = studentService.createStudent(student);
            return java.util.Collections.singletonMap("success", true);
        } catch (RuntimeException e) {
            return java.util.Collections.singletonMap("message", e.getMessage());
        }
    }

    /**
     * 删除学生
     */
    @DeleteMapping("/{id}")
    public boolean deleteStudent(@PathVariable Integer id) {
        return studentService.deleteStudent(id);
    }

    /**
     * 更新学生信息
     */
    @PutMapping("/{id}")
    public Student updateStudent(@PathVariable Integer id, @RequestBody Student student) {
        student.setId(id);
        return studentService.updateStudent(student);
    }

    /**
     * 获取学生信息
     */
    @GetMapping("/{id}")
    public Student getStudent(@PathVariable Integer id) {
        return studentService.getStudentById(id);
    }

    /**
     * 获取所有学生
     */
    @GetMapping
    public List<Student> getAllStudents() {
        return studentService.getAllStudents();
    }

    /**
     * 根据学号查询学生
     */
    @GetMapping("/number/{studentNumber}")
    public Student getStudentByNumber(@PathVariable String studentNumber) {
        return studentService.getStudentByNumber(studentNumber);
    }

    /**
     * 获取学生信息（包含选课信息）
     */
    @GetMapping("/{id}/courses")
    public Student getStudentWithCourses(@PathVariable Integer id) {
        return studentService.getStudentWithCourses(id);
    }

    /**
     * 分页查询学生
     */
    @GetMapping("/page")
    public IPage<Student> getStudentPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword) {
        Page<Student> page = new Page<>(current, size);
        return studentService.getStudentPage(page, keyword);
    }
} 